package com.eascs.web.o2o.web.reward.controller;



import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.app.exception.ControllerException;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
import com.eascs.web.o2o.web.reward.entity.OverdualForm;
import com.eascs.web.o2o.web.reward.entity.Province;
import com.eascs.web.o2o.web.reward.service.BaseService;
import com.eascs.web.o2o.web.reward.service.OverdualReportService;
import com.eascs.web.o2o.web.util.BankRowWriteHandler;

@Controller
@RequestMapping("/overdual")
public class OverdualReportController extends BaseMasterViewController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	OverdualReportService overdualReportService;
	
	@Inject
	BaseService baseService;
	
	

	@Override
	public void preHandle() {
		super.preHandle();
		//ViewContext.put(MenuTag.CURRENT_MENU_CODE, "report");
	}

	
	
	//逾期明细报表查询, 导入, 导出功能
	@RequestMapping(value = "/overdualReportDetail")
	public String overdualReportDetail(OverdualForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "overdualReportDetail");
		String o2ourl = "/overdualRest/overdualReportDetail";
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
		if (entity != null) {
			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String loanDateS = entity.getLoanDateS()== null ? "" : entity.getLoanDateS();
			 String loanDateE= entity.getLoanDateE()== null ? "" : entity.getLoanDateE();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateS", loanDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateE", loanDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
		} else {
			 o2ourl = o2ourl + "?o2oIndex=";
		}
		List<Province> provinces = baseService.findAllEntity(Province.class);
		ViewContext.put("provinces", provinces);
		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		return "overdue/overdualReportDetail";
	}

	// 数据导出
	@RequestMapping("/downloadOverdualReportDetail")
	public void downloadOverdualReportDetail(OverdualForm entity, HttpServletResponse httpServletResponse,
			HttpServletRequest request) {
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = overdualReportService.downOverdualReportDetail(entity);
		saveOverdualReportDetail(pageResult.getData());
		String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/overdualReportstat.xls").toString();
		OutputStream os = null;
		try {
			String outfile= "逾期明细表.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	public String saveOverdualReportDetail(List<Object[]> data) {
		// 模板
		String soucreFileName = "/excelTemplate/overdue_detail.xls";
		// 输出路径
		String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/overdualReportstat.xls").toString();

		// 包含的列
		int col = 19;
		// 需要统计的列
		int[] arr = {};

		// 开始的表格，行，列
		int sheetIndex = 0;
		int rowIndex =1;
		int columnIndex = 0;
		BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

		// 生产excel
		write(soucreFileName, outPath, bankRowWriteHandler);

		return path + "/" + outPath;
	}

	
	
	
	
	
	
	
	//回款明细报表,导入, 导出功能
	@RequestMapping(value = "/receivedPaymentsDetail")
	public String receivedPaymentsDetail(OverdualForm entity) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "receivedPaymentsDetail");
			String o2ourl = "/overdualRest/receivedPaymentsDetail";
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, -1);
			String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
			String month = entity.getMonth()==null?date:entity.getMonth();
			entity.setMonth(month);
			if (entity != null) {
				 String province = entity.getProvince() == null ? "" : entity.getProvince();
				 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
				 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
				 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
				 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
				 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
				 o2ourl = o2ourl + "&o2oIndex=";
			} else {
				 o2ourl = o2ourl + "?o2oIndex=";
			}
			List<Province> provinces = baseService.findAllEntity(Province.class);
			ViewContext.put("provinces", provinces);
			ViewContext.put("o2ourl", o2ourl);
			ViewContext.put("vo", entity);
			return "overdue/receivedPaymentsDetail";
		}

		// 数据导出
		@RequestMapping("/downloadReceivedPaymentsDetail")
		public void downloadReceivedPaymentsDetail(OverdualForm entity, HttpServletResponse httpServletResponse,
				HttpServletRequest request) {
			entity.setPageSize(Integer.MAX_VALUE);
			PageResult<Object[]> pageResult = overdualReportService.downReceivedPaymentsDetail(entity);
			saveReceivedPaymentsDetail(pageResult.getData());
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/receivedpaymentsdetail.xls").toString();
			OutputStream os = null;
			try {
				String outfile= "回款明细表.xls";
				String userAgent = request.getHeader("User-Agent");
				byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
				outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
				File f = new File(outPath);
				if (!f.exists())
					throw new RuntimeIOException("file not found");
				os = httpServletResponse.getOutputStream();
				httpServletResponse.reset();
				httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
				httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				IoUtil.fileToOutputStream(f, byteArrayOutputStream);
				os.write(byteArrayOutputStream.toByteArray());
				os.flush();
			} catch (IOException e) {
				throw new RuntimeIOException(e);
			} finally {
				IoUtil.close(os);
			}
		}

		public String saveReceivedPaymentsDetail(List<Object[]> data) {
			// 模板
			String soucreFileName = "/excelTemplate/received_payments_detail.xls";
			// 输出路径
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/receivedpaymentsdetail.xls").toString();

			// 包含的列
			int col = 19;
			// 需要统计的列
			int[] arr = {};

			// 开始的表格，行，列
			int sheetIndex = 0;
			int rowIndex =1;
			int columnIndex = 0;
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

			// 生产excel
			write(soucreFileName, outPath, bankRowWriteHandler);

			return path + "/" + outPath;
		}

		
		
		
		
		
		
		//当期回款本息金额报表查询, 导入, 导出功能
		@RequestMapping(value = "/receiveOverdueAmount")
		public String receiveOverdueAmount(OverdualForm entity) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "receiveOverdueAmount");
			String o2ourl = "/overdualRest/receiveOverdueAmount";
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, -1);
			String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
			String month = entity.getMonth()==null?date:entity.getMonth();
			entity.setMonth(month);
			if (entity != null) {
				 String province = entity.getProvince() == null ? "" : entity.getProvince();
				 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
				 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
				 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
				 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
				 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
				 o2ourl = o2ourl + "&o2oIndex=";
			} else {
				 o2ourl = o2ourl + "?o2oIndex=";
			}
			List<Province> provinces = baseService.findAllEntity(Province.class);
			ViewContext.put("provinces", provinces);
			ViewContext.put("o2ourl", o2ourl);
			ViewContext.put("vo", entity);
			return "overdue/receiveOverdueAmount";
		}

		// 数据导出
		@RequestMapping("/downloadReceiveOverdueAmount")
		public void downloadReceiveOverdueAmount(OverdualForm entity, HttpServletResponse httpServletResponse,
				HttpServletRequest request) {
			entity.setPageSize(Integer.MAX_VALUE);
			PageResult<Object[]> pageResult = overdualReportService.downReceiveOverdueAmount(entity);
			saveReceiveOverdueAmount(pageResult.getData());
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/receiveoverdueamount.xls").toString();
			OutputStream os = null;
			try {
				String outfile= "当期回款本息金额报表.xls";
				String userAgent = request.getHeader("User-Agent");
				byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
				outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
				File f = new File(outPath);
				if (!f.exists())
					throw new RuntimeIOException("file not found");
				os = httpServletResponse.getOutputStream();
				httpServletResponse.reset();
				httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
				httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				IoUtil.fileToOutputStream(f, byteArrayOutputStream);
				os.write(byteArrayOutputStream.toByteArray());
				os.flush();
			} catch (IOException e) {
				throw new RuntimeIOException(e);
			} finally {
				IoUtil.close(os);
			}
		}

		public String saveReceiveOverdueAmount(List<Object[]> data) {
			// 模板
			String soucreFileName = "/excelTemplate/receive_overdue_amount.xls";
			// 输出路径
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/receiveoverdueamount.xls").toString();

			// 包含的列
			int col = 22;
			// 需要统计的列
			int[] arr = {};

			// 开始的表格，行，列
			int sheetIndex = 0;
			int rowIndex =1;
			int columnIndex = 0;
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

			// 生产excel
			write(soucreFileName, outPath, bankRowWriteHandler);

			return path + "/" + outPath;
		}
		
		//当期回款本息金额报表查询查询, 导入, 导出功能
		@RequestMapping(value = "/curOverdueAmount")
		public String curOverdueAmount(OverdualForm entity) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "curOverdueAmount");
			String o2ourl = "/overdualRest/curOverdueAmount";
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, -1);
			String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
			String month = entity.getMonth()==null?date:entity.getMonth();
			entity.setMonth(month);
			if (entity != null) {
				 String province = entity.getProvince() == null ? "" : entity.getProvince();
				 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
				 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
				 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
				 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
				 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
				 o2ourl = o2ourl + "&o2oIndex=";
			} else {
				 o2ourl = o2ourl + "?o2oIndex=";
			}
			List<Province> provinces = baseService.findAllEntity(Province.class);
			ViewContext.put("provinces", provinces);
			ViewContext.put("o2ourl", o2ourl);
			ViewContext.put("vo", entity);
			return "overdue/curOverdueAmount";
		}

		// 数据导出
		@RequestMapping("/downloadCurOverdueAmount")
		public void downloadCurOverdueAmount(OverdualForm entity, HttpServletResponse httpServletResponse,
				HttpServletRequest request) {
			entity.setPageSize(Integer.MAX_VALUE);
			PageResult<Object[]> pageResult = overdualReportService.downCurOverdueAmount(entity);
			saveCurOverdueAmount(pageResult.getData());
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/curoverdueamount.xls").toString();
			OutputStream os = null;
			try {
				String outfile= "当期逾期本息金额报表.xls";
				String userAgent = request.getHeader("User-Agent");
				byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
				outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
				File f = new File(outPath);
				if (!f.exists())
					throw new RuntimeIOException("file not found");
				os = httpServletResponse.getOutputStream();
				httpServletResponse.reset();
				httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
				httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				IoUtil.fileToOutputStream(f, byteArrayOutputStream);
				os.write(byteArrayOutputStream.toByteArray());
				os.flush();
			} catch (IOException e) {
				throw new RuntimeIOException(e);
			} finally {
				IoUtil.close(os);
			}
		}

		public String saveCurOverdueAmount(List<Object[]> data) {
			// 模板
			String soucreFileName = "/excelTemplate/cur_overdue_amount.xls";
			// 输出路径
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/curoverdueamount.xls").toString();

			// 包含的列
			int col = 23;
			// 需要统计的列
			int[] arr = {};

			// 开始的表格，行，列
			int sheetIndex = 0;
			int rowIndex =1;
			int columnIndex = 0;
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

			// 生产excel
			write(soucreFileName, outPath, bankRowWriteHandler);

			return path + "/" + outPath;
		}
		
		
		//当期回款本息金额报表查询查询, 导入, 导出功能
		@RequestMapping(value = "/rewardSalaryCheck")
		public String rewardSalaryCheck(OverdualForm entity) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "rewardSalaryCheck");
			String o2ourl = "/overdualRest/rewardSalaryCheck";
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, -1);
			String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
			String month = entity.getMonth()==null?date:entity.getMonth();
			entity.setMonth(month);
			if (entity != null) {
				 String province = entity.getProvince() == null ? "" : entity.getProvince();
				 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
				 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
				 o2ourl = o2ourl + "&o2oIndex=";
			} else {
				 o2ourl = o2ourl + "?o2oIndex=";
			}
			List<Province> provinces = baseService.findAllEntity(Province.class);
			ViewContext.put("provinces", provinces);
			ViewContext.put("o2ourl", o2ourl);
			ViewContext.put("vo", entity);
			return "overdue/rewardSalaryCheck";
		}

		// 数据导出
		@RequestMapping("/downloadRewardSalaryCheck")
		public void downloadRewardSalaryCheck(OverdualForm entity, HttpServletResponse httpServletResponse,
				HttpServletRequest request) {
			entity.setPageSize(Integer.MAX_VALUE);
			PageResult<Object[]> pageResult = overdualReportService.downloadRewardSalaryCheck(entity);
			saveRewardSalaryCheck(pageResult.getData());
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/rewardsalarycheck.xls").toString();
			OutputStream os = null;
			try {
				String outfile= "薪酬核算.xls";
				String userAgent = request.getHeader("User-Agent");
				byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
				outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
				File f = new File(outPath);
				if (!f.exists())
					throw new RuntimeIOException("file not found");
				os = httpServletResponse.getOutputStream();
				httpServletResponse.reset();
				httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
				httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				IoUtil.fileToOutputStream(f, byteArrayOutputStream);
				os.write(byteArrayOutputStream.toByteArray());
				os.flush();
			} catch (IOException e) {
				throw new RuntimeIOException(e);
			} finally {
				IoUtil.close(os);
			}
		}

		public String saveRewardSalaryCheck(List<Object[]> data) {
			// 模板
			String soucreFileName = "/excelTemplate/reward_salary_check.xls";
			// 输出路径
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/rewardsalarycheck.xls").toString();

			// 包含的列
			int col = 21;
			// 需要统计的列
			int[] arr = {};

			// 开始的表格，行，列
			int sheetIndex = 0;
			int rowIndex =1;
			int columnIndex = 0;
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

			// 生产excel
			write(soucreFileName, outPath, bankRowWriteHandler);

			return path + "/" + outPath;
		}
		
		
		
		
		
		//放款天数汇总报表查询, 导入, 导出功能
		@RequestMapping(value = "/evaluateLoanDay")
		public String evaluateLoanDay(OverdualForm entity) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "evaluateLoanDay");
			String o2ourl = "/overdualRest/evaluateLoanDay";
			if (entity != null) {
				 String province = entity.getProvince() == null ? "" : entity.getProvince();
				 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
				 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
				 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
				 o2ourl = o2ourl + "&o2oIndex=";
			} else {
				 o2ourl = o2ourl + "?o2oIndex=";
			}
			List<Province> provinces = baseService.findAllEntity(Province.class);
			ViewContext.put("provinces", provinces);
			ViewContext.put("o2ourl", o2ourl);
			ViewContext.put("vo", entity);
			return "evaluate/evaluateLoanDay";
		}

		// 数据导出
		@RequestMapping("/downloadEvaluateLoanDay")
		public void downloadEvaluateLoanDay(OverdualForm entity, HttpServletResponse httpServletResponse,
				HttpServletRequest request) {
			entity.setPageSize(Integer.MAX_VALUE);
			PageResult<Object[]> pageResult = overdualReportService.evaluateLoanDay(entity);
			saveEvaluateLoanDay(pageResult.getData());
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/evaluateloanday.xls").toString();
			OutputStream os = null;
			try {
				String outfile= "放款天数汇总报表.xls";
				String userAgent = request.getHeader("User-Agent");
				byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
				outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
				File f = new File(outPath);
				if (!f.exists())
					throw new RuntimeIOException("file not found");
				os = httpServletResponse.getOutputStream();
				httpServletResponse.reset();
				httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
				httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				IoUtil.fileToOutputStream(f, byteArrayOutputStream);
				os.write(byteArrayOutputStream.toByteArray());
				os.flush();
			} catch (IOException e) {
				throw new RuntimeIOException(e);
			} finally {
				IoUtil.close(os);
			}
		}

		public String saveEvaluateLoanDay(List<Object[]> data) {
			// 模板
			String soucreFileName = "/excelTemplate/evaluate_loan_day.xls";
			// 输出路径
			String path = OverdualReportController.class.getResource("/excelTemplate").getPath();
			StringBuffer s = new StringBuffer();
			String outPath = s.append(path).append("/evaluateloanday.xls").toString();

			// 包含的列
			int col = 3;
			// 需要统计的列
			int[] arr = {1,2};

			// 开始的表格，行，列
			int sheetIndex = 0;
			int rowIndex =1;
			int columnIndex = 0;
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList("考核月份"),sheetIndex, rowIndex, columnIndex);

			// 生产excel
			write(soucreFileName, outPath, bankRowWriteHandler);

			return path + "/" + outPath;
		}
		
				
				
				
				
		
	/*
	 * 生成excel 参数：数据，源文件，输出文件，excel包含的列，需要统计的列，excel标题
	 */
	public void write(String soucreFileName, String outPath, BankRowWriteHandler bankRowWriteHandler) {
		IoUtil.createDirectorieIfMissing(new File(outPath).getParentFile());
		try {
			ExcelApiHelper.writeHandle(new FileOutputStream(outPath),
					OverdualReportController.class.getResourceAsStream(soucreFileName), bankRowWriteHandler);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ControllerException(e);
		}
	}
}
